package com.example.car_lrp_plugin

import android.content.Context
import android.graphics.Bitmap
import androidx.annotation.NonNull
import com.pcl.ocr.ui.LPRUtils
import com.pcl.ocr.utils.toByteArray
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** CarLrpPlugin */
class CarLrpPlugin : FlutterPlugin, MethodCallHandler {

    private lateinit var channel: MethodChannel
    private lateinit var context: Context

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "car_lrp_plugin")
        context = flutterPluginBinding.applicationContext
        flutterPluginBinding.platformViewRegistry.registerViewFactory("plugins.flutter.io/car_lpr_view",
            CarLprFactory())
        channel.setMethodCallHandler(this)
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        when (call.method) {
            "getPlatformVersion" -> {
                result.success("Android ${android.os.Build.VERSION.RELEASE}")
            }
            "openScan" -> {
                val option = call.arguments as Map<*, *>
                val needApply = option["needApply"] as Boolean
                LPRUtils.openScan(context, needApply){code ->
                    val map = mapOf(
                        "code" to code
//                        "frame" to  image!!.toByteArray()
                    )
                    result.success(map)
                }
            }
            else -> {
                result.notImplemented()
            }
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

}
